package com.csdj.rest.controller;

import com.csdj.beans.interfaces.user.IUserService;
import com.csdj.common.WebResult;
import com.csdj.common.constant.WebResultConstant;
import com.csdj.middleware.RocketMqProducer;
import com.csdj.middleware.config.MqttConfig;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping("/test")
public class TestController {

//    @DubboReference
    private IUserService userService;
    @Resource
    private MqttConfig.MyGateway myGateway;

    @Resource
    private RocketMqProducer rocketMqProducer;

    @GetMapping("/")
    public WebResult test(HttpServletRequest request){
        return new WebResult(200,"test for springboot")
                .putData("name","cloud");
    }

    @GetMapping("/user/{id}")
    public WebResult user(@PathVariable int id){
        return new WebResult(200,userService.getById(id));
    }

    @GetMapping("/mqtt")
    public WebResult mqtt(String msg){
        myGateway.sendToMqtt("test for mqtt in java");
        return WebResult.success("good");
    }

    @GetMapping("/rocket")
    public WebResult rocketSendTest(String msg){
        final SendResult sendResult = rocketMqProducer.sendMsg(msg);
        if(sendResult != null){
            if(sendResult.getSendStatus().compareTo(SendStatus.SEND_OK) == 0) {
                return WebResult.success(true);
            }
        }
        return WebResult.fail(WebResultConstant.FAIL);
    }

    @GetMapping("/rocket/tran")
    public WebResult rocketTransaction(String msg){
        SendResult sendResult = rocketMqProducer.sendTranscationMsg("test-msg","tag1",msg,null);
        if(sendResult != null){
            if(sendResult.getSendStatus().compareTo(SendStatus.SEND_OK) == 0) {
                return WebResult.success(true);
            }
        }
        return WebResult.fail(WebResultConstant.FAIL);
    }
    @PostMapping("/rocket/{topic}/{tag}")
    public WebResult rocketMsg(@PathVariable("topic")String topic,@PathVariable("tag")String tag,
                               @RequestParam(value="msg")String msg,@RequestParam("order")long order){
        final SendResult sendResult = rocketMqProducer.sendMsgByOrder(topic, tag, msg,order);
        if(sendResult != null){
            if(sendResult.getSendStatus().compareTo(SendStatus.SEND_OK) == 0) {
                return WebResult.success(true);
            }
        }
        return WebResult.fail(WebResultConstant.FAIL);
    }

    @GetMapping("lang")
    public WebResult testLang(@RequestParam("lang")String lang){
        if("zh".equals(lang)){
            return WebResult.fail(WebResultConstant.ORDER_NOT_EXISTS, Locale.CHINA);
        }
        if("en".equals(lang)){
            return WebResult.fail(WebResultConstant.ORDER_NOT_EXISTS,Locale.US);
        }
        return WebResult.fail(WebResultConstant.ORDER_NOT_EXISTS);
    }

    @GetMapping("/lang/header")
    public WebResult testLangInHeader(@Header("lang")String lang){
        if("zh".equals(lang)){
            return WebResult.fail(WebResultConstant.ORDER_NOT_EXISTS, Locale.CHINA);
        }
        if("en".equals(lang)){
            return WebResult.fail(WebResultConstant.ORDER_NOT_EXISTS,Locale.US);
        }
        return WebResult.fail(WebResultConstant.ORDER_NOT_EXISTS);
    }


}
